#include <iostream>
#include <stdio.h>

#include "MSR206.h"

#define DEVICE "/dev/cu.usbserial"

MSR206 *msr = new MSR206();

void sigproc(int x)
{ 		
		printf("Quitting...");
		msr->disconnect();
		delete(msr);
		exit(0);
}

void printTrack(char *msg, unsigned char *buf, unsigned int len)
{
	printf("[%s]: ", msg);
	for(int x = 0; x < len; x++) printf("%c", buf[x]);
	printf("\n");
}

int main (int argc, char * const argv[]) {
	magnetic_stripe_t *data = NULL;
	
	signal(SIGINT, sigproc);
	
	try 
	{
		/* connect to specified device */
		msr->connect(DEVICE);
		printf("Connected to %s\n", DEVICE);
		
		/* initialize the msr */
		msr->init();
		printf("Initialized MSR206.\n");
		
		/* read card */
		while(1) {
		
		printf("Waiting for swipe...\n");
		data = msr->readCard(TRACK_7BIT, TRACK_5BIT, TRACK_7BIT);
		
		printTrack("Track 1", data->track1, data->t1_len);
		printTrack("Track 2", data->track2, data->t2_len);
		printTrack("Track 3", data->track3, data->t3_len);
		
		msr->free_ms_data(data);
		
		}
				
		/* close connection */
		msr->disconnect();

	}
	catch(const char *msg)
	{
		printf("MSR206 Error: %s\n", msg);
	}
	
	
	delete(msr);
    
	return 0;
}
